package com.koala.learn.threadlearn.charter4;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @Description TODO
 * @Date 2022/2/27 21:29
 * @Created by koala
 */

public class DefaultThreadPool<Job extends Runnable> implements ThreadPool<Job> {

    // 最大的工作人数
    public static final int MAX_WORKER_NUMBERS = 10;

    public static final int DEFAULT_WORKER_NUMBERS = 5;

    public static final int MIN_WORKER_NUMBERS = 1;

    private final AtomicLong threadNum = new AtomicLong();

    private int workNum = DEFAULT_WORKER_NUMBERS;

    private final LinkedList<Job> jobs = new LinkedList<>();

    private final List<Worker> workers = Collections.synchronizedList(new ArrayList<Worker>());


    private void initWorkers(int num) {
        for (int i = 0; i < num; i++) {
            Worker worker = new Worker();
            workers.add(worker);
            Thread thread = new Thread(worker, "Thread-" + threadNum.incrementAndGet());
            thread.start();
        }
    }

    public DefaultThreadPool(int num) {
        initWorkers(num);
    }

    @Override
    public void execute(Job job) {
        if (job != null) {
            synchronized (jobs) {
                jobs.addLast(job);
                jobs.notifyAll();
            }
        }
    }

    @Override
    public void shutdown() {
        for (Worker worker : workers) {
            worker.shutdown();
        }
    }

    @Override
    public void addWorker(int num) {
        synchronized (jobs) {
            if (num + this.workNum > MAX_WORKER_NUMBERS) {
                num = MAX_WORKER_NUMBERS - workNum;
            }
            initWorkers(num);
            this.workNum += num;
        }
    }

    @Override
    public void removeWorker(int num) {
        synchronized (jobs) {
            if (num > workNum) {
                throw new IllegalArgumentException("beyond worknum");
            }
            int count = 0;
            while (count < num) {
                Worker worker = workers.get(count);
                if (workers.remove(worker)) {
                    worker.shutdown();
                    count++;
                }
            }
            this.workNum -= count;
        }
    }

    @Override
    public int getJobSize() {
        return jobs.size();
    }

    class Worker implements Runnable {
        private volatile boolean running = true;

        @Override
        public void run() {
            while (running) {
                Job job = null;
                synchronized (jobs) {
                    while (jobs.isEmpty()) {
                        try {
                            jobs.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    job = jobs.removeFirst();
                }
                if (job != null) {
                    job.run();
                }

            }
        }

        public void shutdown() {
            running = false;
        }

    }
}
